/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ufpb.halloc.dados;

import br.com.ufpb.halloc.conexaoDB.Pool;
import br.com.ufpb.halloc.conexaoDB.PoolIF;
import br.com.ufpb.halloc.negocio.Usuario;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Kawe Ramon
 */
public class UsuarioDAO {
 
    Connection connection = null;
    private static UsuarioDAO usuarioDAO;
    PoolIF pool;
    
    
    public static UsuarioDAO getInstance(){
        if(usuarioDAO == null){
            usuarioDAO = new UsuarioDAO();
        }
        return usuarioDAO;
    }
    public UsuarioDAO(){
        pool = new Pool();
    }
    
    public void cadastrar(Usuario user){
        Connection con = pool.getConnection();
        PreparedStatement pr;
        
        try {
            pr = con.prepareStatement("INSERT INTO usuario VALUES(?, ?, ?);");
            pr.setString(1, user.getLogin());
            pr.setString(2, user.getSenha());
            pr.setString(3, user.getId());
            pr.execute();
            pr.close();
        } catch (SQLException ex) {
            Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            pool.liberarConnection(con);
        }
        
    }
    
    public boolean getUsurario(String login, String senha){
        boolean resultado = false;
        Connection c = pool.getConnection();
        PreparedStatement ps;
        ResultSet rs;
        try{
            ps = c.prepareStatement("select * from usuario;");
            rs = ps.executeQuery();
            String log;
            String pass;
            while(rs.next()){
                log = rs.getString("login");
                pass = rs.getString("senha");
                if (log.equals(login) && pass.equals(senha)){                
                    resultado = true;
                    break;
                }
            }
        }
        catch (SQLException e){
            e.printStackTrace();
        }finally{
            pool.liberarConnection(c);
        }
        return resultado;
    }
    
    
}
